package com.example.service.wechat.impl;

import com.example.mapper.wechat.BrokenBikeMapper;
import com.example.pojo.web.Result;
import com.example.pojo.wechat.BrokenBike;
import com.example.pojo.wechat.OpenId_SessionKey;
import com.example.service.wechat.BrokenBikeService;
import com.example.service.wechat.WxAuthenticService;
import com.example.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName BrokenBikeServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/19 - 04 - 19 - 11:56
 * @version: 1.0
 */
@Service
public class BrokenBikeServiceImpl implements BrokenBikeService {
    //properties
    @Autowired
    private BrokenBikeMapper brokenBikeMapper;
    @Autowired
    private WxAuthenticService wxAuthenticService;

    //methods
    @Override
    public List<BrokenBike> selectBrokenBikesAll() {
        try {
            return brokenBikeMapper.selectBrokenBikesAll();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Integer addBrokenBikeRecord(BrokenBike brokenBike,String code) {
        try {
            //根据code生成opendid
            OpenId_SessionKey openId_sessionKey = wxAuthenticService.getOpenId_SessionKey(code);
            String openid = openId_sessionKey.getOpenid();
            brokenBike.setUploader(openid);
            return brokenBikeMapper.addBrokenBikeRecord(brokenBike);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Integer updateBrokenBikeStatusByID(Integer id, Integer status) {
        try {
            return brokenBikeMapper.updateBrokenBikeStatusByID(id, status);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result updateBrokenBikeByID(Integer id) {
        try {
            Integer rows = brokenBikeMapper.updateBrokenBikeStatusByID(id, 1);
            if (rows>0){
                return ResultUtil.success(1,"处理成功");
            }else {
                return ResultUtil.error(0,"处理失败");
            }
        }catch (Exception e){
            return ResultUtil.error(-1,"系统异常");
        }
    }
}
